<template>
	<view class="sort">
		<!-- 搜索框区域 -->
		<my-search></my-search>
		<!-- 分类页 -->
		<view class="sort-nav">
			<!-- 左侧导航区域 -->
			<view class="sort-nav-left">
				<scroll-view scroll-y class="left-scroll-view">
					<view :class="['scroll-view-item',active == index ? 'active' : '']"
						v-for="(item,index) in sortNavList" :key="item.cat_id" @click="showRightSort(item,index)">
						{{item.cat_name}}</view>
				</scroll-view>
			</view>
			<!-- 右侧展示区域 -->
			<view class="sort-nav-right">
				<scroll-view class="right-scroll-view" scroll-y :scroll-top="scrollTop">
					<view class="sort-nav-right-box" v-for="(item1,index1) in sortChildrenList" :key="index1">
						<view class="box-title">
							/ {{item1.cat_name}} /
						</view>
						<view class="box-body">
							<view class="body-item" v-for="(item2,index2) in item1.children" :key="index2"
								@click="goToLists(item2.cat_id)">
								<image :src="item2.cat_icon" class="body-item-img"></image>
								<text>{{item2.cat_name}}</text>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import badgeMixin from '../../mixins/tabbarbage.js'
	export default {
		data() {
			return {
				// 分类一级导航数据
				sortNavList: [],
				// 活动项
				active: 0,
				// 分类子数据
				sortChildrenList: [],
				// 滚动条位置
				scrollTop: 0
			};
		},
		mixins:[badgeMixin],
		methods: {
			// 获取分类页数据
			async getSortList() {
				const {
					data: res
				} = await uni.$http.get("/api/public/v1/categories");
				if (res.meta.status !== 200) return uni.$showToast();
				this.sortNavList = res.message;
				this.sortChildrenList = res.message[0].children;
			},
			// 展示渲染右侧分类
			showRightSort(item, index) {
				this.active = index;
				this.sortChildrenList = item.children;
				this.scrollTop = this.scrollTop === 0 ? 1 : 0;
			},
			// 跳转到商品列表页
			goToLists(id) {
				uni.navigateTo({
					url: `/subpage/lists/lists?cid=${id}`
				})
			}
		},
		onLoad() {
			// 获取分类页数据
			this.getSortList()
		}
	}
</script>

<style lang="less">
	page {
		height: 100%;
	}

	.sort {
		height: 100%;

		.sort-nav {
			height: 100%;
			display: flex;

			// 左侧导航区域
			.sort-nav-left {
				height: 100%;

				.left-scroll-view {
					height: 100%;

					.scroll-view-item {
						width: 90px;
						height: 120rpx;
						line-height: 120rpx;
						text-align: center;
						font-size: 24rpx;
						background-color: #f7f7f7;
					}

					.active {
						position: relative;
						background-color: #fff;

						&::before {
							position: absolute;
							left: 0;
							top: 50%;
							transform: translateY(-50%);
							content: '';
							display: block;
							width: 3px;
							height: 50%;
							background-color: #c00000;
						}
					}
				}
			}

			// 右侧展示区域
			.sort-nav-right {
				width: 100%;
				height: 100%;

				.right-scroll-view {
					height: 100%;
					.sort-nav-right-box {
						height: 100%;

						.box-title {
							height: 120rpx;
							width: 100%;
							line-height: 120rpx;
							text-align: center;
							font-size: 24rpx;
							font-weight: bold;
						}

						.box-body {
							display: flex;
							flex-wrap: wrap;

							.body-item {
								width: 33.33%;
								font-size: 24rpx;
								display: flex;
								flex-direction: column;
								justify-content: center;
								align-items: center;
								padding-bottom: 20rpx;

								.body-item-img {
									display: block;
									width: 120rpx;
									height: 120rpx;
									margin-bottom: 5rpx;
								}
							}
						}
					}
				}
			}
		}
	}
</style>
